﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
using Wimm.Common.Entities;
using Wimm.Common.Entities.Utils;
using Wimm.Common.Interfaces.Services;
using Wimm.CustomContols;
using Wimm.UI.Common;
using Wimm.UI.Login;
using Wimm.Common;

namespace Wimm.UI.ViewModels
{
    public class RegistrationViewModel: ViewModelBase
    {
        private String _userName;
        private String _password;
        private String _errorMessage;

        private readonly INavigationService _navigationService;
        private readonly IUserService _userService;

        private readonly RelayCommand _createCommand;
        public RegistrationViewModel(INavigationService navigationService, IUserService userService)
        {
            _createCommand = new RelayCommand(DoCreate, CanCreate);

            _navigationService = navigationService;
            _userService = userService;
            this.ErrorMessage = "";

            this.Language = Windows.Globalization.ApplicationLanguages.Languages.First() ?? "en-US";
        }

        public ICommand CreateCommand
        {
            get { return _createCommand; }
        }

        private bool CanCreate()
        {
            return !String.IsNullOrWhiteSpace(_userName) && !String.IsNullOrWhiteSpace(_password) &&
                   _userName.Length > 0 && _password.Length > 0;
        }

        private async void DoCreate()
        {
            User newUser = new User(_userName, _password);

            if (await _userService.DoublLogin(_userName))
            {
                if (await _userService.DoublPassword(_password))
                {
                    await _userService.InsertUser(newUser);

                    this.UserName = "";
                    this.Password = "";

                    this.ErrorMessage = "RegistrationWelcome".GetStringFromResource(this.Language);
                    await Task.Delay(2000);

                    Messenger.Default.Send<MultiLoginUpdateMessenger>(new MultiLoginUpdateMessenger());

                    _navigationService.NavigateTo("multi", typeof (MultiLogin));
                }
            }

            this.ErrorMessage = "RegistrationError".GetStringFromResource(this.Language);
        }

        public String Password
        {
            get
            {
                return _password;
            }
            set
            {
                Set<String>(() => Password, ref _password, value);
                _createCommand.RaiseCanExecuteChanged();
            }
        }

        public String UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                Set<String>(() => UserName, ref _userName, value);
                _createCommand.RaiseCanExecuteChanged();
            }
        }

        public String ErrorMessage
        {
            get
            {
                return _errorMessage;
            }
            set
            {
                Set<String>(() => ErrorMessage, ref _errorMessage, value);
            }
        }
        public string Language { get; set; }
    }
}
